//
//  Direction.swift
//  XCDragCardContainer
//  Created by XC on 2023/3/1.
//
//
//

import Foundation

public enum Direction: Int, CaseIterable {
    case left
    case right
    case up
    case down
    
    public static let horizontal: [Direction] = [.left, .right]
    public static let vertical: [Direction] = [.up, .down]
    public static let all: [Direction] = Direction.allCases
    
    public var vector: CGVector {
        switch self {
            case .left:
                return CGVector(dx: -1, dy: 0)
            case .right:
                return CGVector(dx: 1, dy: 0)
            case .up:
                return CGVector(dx: 0, dy: -1)
            case .down:
                return CGVector(dx: 0, dy: 1)
        }
    }
}

extension Direction: CustomStringConvertible {
    public var description: String {
        switch self {
            case .left:
                return "Left"
            case .right:
                return "Right"
            case .up:
                return "Up"
            case .down:
                return "Down"
        }
    }
}

extension Direction {
    public static func fromPoint(_ point: CGPoint) -> Direction {
        switch (point.x, point.y) {
            case let (x, y) where abs(x) >= abs(y) && x > 0:
                return .right
            case let (x, y) where abs(x) >= abs(y) && x < 0:
                return .left
            case let (x, y) where abs(x) < abs(y) && y < 0:
                return .up
            case let (x, y) where abs(x) < abs(y) && y > 0:
                return .down
            case (_, _):
                return .right
        }
    }
}
